#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
import sys

def MapBuildDir(list):
    return map(lambda x: '#/' + Dir('.').path + '/../' + x, list)

env = BuildEnv.Clone()

env.CppDisableExceptions()

# Generate the source files
SandeshGenFiles  = env.SandeshGenCpp('bgp_peer.sandesh')
SandeshGenFiles += env.SandeshGenOnlyCpp('bgp_peer_internal.sandesh')
SandeshGenFiles += env.SandeshGenCpp('bgp_log.sandesh')
SandeshGenFiles += env.SandeshGenCpp('virtual_network.sandesh')
SandeshGenFiles += env.SandeshGenCpp('vrouter.sandesh')
SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)

env.Append(CPPPATH = env['TOP'])
env.Append(CPPPATH = [env['TOP'] + '/bgp'])
env.Append(CPPPATH = [env['TOP'] + '/io'])
env.Append(CPPPATH = [env['TOP'] + '/db'])
env.Append(CPPPATH = [env['TOP'] + '/ifmap'])
env.Append(CPPPATH = [env['TOP'] + '/config-client-mgr'])
env.Append(CPPPATH = [env['TOP'] + '/base/sandesh'])
env.Append(CPPPATH = [env['TOP'] + '/tools/sandesh/library/common'])

SandeshPeerFiles = env.SandeshGenCpp('peer_info.sandesh')
SandeshPeerGenSrcs = env.ExtractCpp(SandeshPeerFiles)
libpeer_sandesh = env.Library('peer_sandesh', SandeshPeerGenSrcs);

SandeshTableFiles = env.SandeshGenCpp('sandesh_info.sandesh')
SandeshTableGenSrcs = env.ExtractCpp(SandeshTableFiles)
libsandesh_sandesh = env.Library('sandesh_sandesh', SandeshTableGenSrcs);

libbgp = env.Library('bgp',
                     SandeshGenSrcs +
                     ['bgp_aspath.cc',
                      'bgp_attr.cc',
                      'bgp_attr_base.cc',
                      'bgp_config.cc',
                      'bgp_condition_listener.cc',
                      'bgp_debug.cc',
                      'bgp_evpn.cc',
                      'bgp_export.cc',
                      'bgp_factory.cc',
                      'bgp_lifetime.cc',
                      'bgp_log.cc',
                      'bgp_membership.cc',
                      'bgp_message_builder.cc',
                      'bgp_multicast.cc',
                      'bgp_mvpn.cc',
                      'bgp_origin_vn_path.cc',
                      'bgp_path.cc',
                      'bgp_peer.cc',
                      'bgp_peer_close.cc',
                      'bgp_peer_key.cc',
                      'bgp_proto.cc',
                      'bgp_rib_policy.cc',
                      'bgp_ribout.cc',
                      'bgp_ribout_updates.cc',
                      'bgp_route.cc',
                      'bgp_sandesh.cc',
                      'bgp_server.cc',
                      'bgp_session_manager.cc',
                      'bgp_session.cc',
                      'bgp_show_config.cc',
                      'bgp_show_evpn_table.cc',
                      'bgp_show_multicast_manager.cc',
                      'bgp_show_mvpn_manager.cc',
                      'bgp_show_mvpn_project_manager.cc',
                      'bgp_show_neighbor.cc',
                      'bgp_show_ribout_statistics.cc',
                      'bgp_show_route.cc',
                      'bgp_show_route_summary.cc',
                      'bgp_show_routing_instance.cc',
                      'bgp_show_rtarget_group.cc',
                      'bgp_table.cc',
                      'bgp_update.cc',
                      'bgp_update_monitor.cc',
                      'bgp_update_queue.cc',
                      'bgp_update_sender.cc',
                      'community.cc',
                      'message_builder.cc',
                      'peer_close_manager.cc',
                      'peer_stats.cc',
                      'state_machine.cc',
                      ])

# ifmap based BGP configuration used by the control-node
libbgp_ifmap_config = env.Library('bgp_ifmap_config',
                                  [
                                      'bgp_config_ifmap.cc',
                                      'bgp_config_listener.cc',
                                      'bgp_config_parser.cc',
                                      'bgp_ifmap_sandesh.cc',
                                      'bgp_show_ifmap_peering_config.cc',
                                  ])

libbgp_xmpp = env.Library('bgp_xmpp',
                          [
                              'bgp_xmpp_channel.cc',
                              'bgp_xmpp_peer_close.cc',
                              'bgp_xmpp_sandesh.cc',
                              'xmpp_message_builder.cc',
                              'bgp_xmpp_rtarget_manager.cc',
                          ])

libbgp_yaml_config = env.Library('bgp_yaml_config',
                                 [
                                     'bgp_config_yaml.cc',
                                 ])

test_suite = env.SConscript('ermvpn/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('evpn/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('inet/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('inet6/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('inet6vpn/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('l3vpn/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('mvpn/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('origin-vn/SConscript', exports='BuildEnv',
                             duplicate = 0)
env.SConscript('routing-policy/SConscript', exports='BuildEnv', duplicate = 0)

env.SConscript('routing-instance/SConscript', exports='BuildEnv', duplicate = 0)
test_suite += env.SConscript('rtarget/SConscript', exports='BuildEnv',
                             duplicate = 0)
env.SConscript('security_group/SConscript', exports='BuildEnv', duplicate = 0)
test_suite += env.SConscript('tunnel_encap/SConscript', exports='BuildEnv',
                             duplicate = 0)
test_suite += env.SConscript('extended-community/SConscript', exports='BuildEnv',
                             duplicate = 0)

test_suite += env.SConscript('test/SConscript',
                             exports='BuildEnv', duplicate = 0)

env.SConscript('daemon/SConscript', exports='BuildEnv', duplicate = 0)

def code_coverage(target, source, env):
    # rm -rf bgp/test_coverage
    import shutil
    shutil.rmtree(target[0].path, ignore_errors = True)

    # lcov --base-directory $ROOT -- directory . --zerocounters -q
    import os
    os.system('lcov --base-directory . --directory ' + Dir('.').path +
              ' --zerocounters -q')
    # execute tests
    import subprocess
    ShEnv = {env['ENV_SHLIB_PATH']: 'build/lib'}

    for test in test_suite:
        cmd = test[0].path
        logfile = open(cmd + '.log', 'w')
        print(cmd)
        subprocess.call([cmd], stdout=logfile, env=ShEnv)

    # lcov --base-directory $ROOT -- directory . -c -o bgp_test.info
    os.system('lcov --base-directory . --directory ' + Dir('.').path +
              ' -c -o bgp_test.info')

    # genhtml -o bgp/test_coverage bgp_test.info
    os.system('genhtml -o ' + target[0].path +
              ' -t "test coverage" --num-spaces 4 bgp_test.info')


if env['OPT'] == 'coverage':
    test_coverage = env.Command(Dir('test_coverage'), '', code_coverage)
    env.AlwaysBuild(test_coverage)
    env.Alias('src/bgp:test_coverage', test_coverage)

# Local Variables:
# mode: python
# End:
